#include "beaneater.h"
#include<qrect.h>
#include "ui_beaneater.h"
#include<qtimer.h>
#include<QKeyEvent>
#include<qstring.h>
beaneater::beaneater(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::beaneater),blsRun(false),speed(500)
{
    ui->setupUi(this);
    ui->mainToolBar->hide();
    ui->statusBar->hide();
    ui->menuBar->hide();
    this->setGeometry(QRect(600,300,700,500));
}

beaneater::~beaneater()
{
    delete ui;
}
void beaneater::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    if(!blsRun){
        Initbeaneater();
    }
    //Q_UNUSED(event);
    //QPainter p(this);
    //p.drawPixmap(0,0,500,500,QPixmap(":/resources/back.png"));
    //p.drawRect(rect());
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(10,10,480,480);
    painter.drawPixmap(10,10,480,480,QPixmap(":/resources/resources/back.png"));
    painter.setPen(Qt::black);

    // painter.setBrush(Qt::green);

    // painter.drawRect(vbeaneaterRect);
    if(frame%2)
    painter.drawPixmap(vbeaneaterRect,QPixmap(":/resources/resources/close.png"));
    else
    painter.drawPixmap(vbeaneaterRect,QPixmap(":/resources/resources/open.png"));

    painter.drawPixmap(food,QPixmap(":/resources/resources/bean.png"));
    QFont font1("Courier",30);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(190,270,sDisplay);
    QFont font("Courier",15);
    painter.setFont(font);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(510,400,scoreLabel);
    painter.drawText(510,450,timerLabel);
    painter.drawText(600,400,QString::number(nScore));
    painter.drawText(600,450,QString::number(deathCountdown));
    if(blsOver)
        timer->stop();
}
void beaneater::Initbeaneater(){
    nDirection=2;
    blsRun=true;
    blsOver=false;
    scoreLabel="SCORE:";
    timerLabel="TIME:";
    nScore=0;
    food=CreateRect();
    QRect rect(240,240,20,20);
    vbeaneaterRect=rect;
    timer=new QTimer(this);
    timer->start(100);
    connect(timer,SIGNAL(timeout()),SLOT(beaneater_update()));
    deathTimer = new QTimer(this);
    deathTimer->start(1000);
    connect(deathTimer,SIGNAL(timeout()),this,SLOT(deathTimeout()));
}
void beaneater::keyPressEvent(QKeyEvent *event){
    QKeyEvent*key=(QKeyEvent*)event;
    switch(key->key()){
    case Qt::Key_Up:nDirection=1;
        break;
    case Qt::Key_Down:nDirection=2;
        break;
    case Qt::Key_Left:nDirection=3;
        break;
    case Qt::Key_Right:nDirection=4;
        break;
    default:;
    }
}
void beaneater::beaneater_update(){
    IsEat();
    frame++;
    switch(nDirection){
    case 1:
        vbeaneaterRect.setTop(vbeaneaterRect.top()-10);
        vbeaneaterRect.setBottom(vbeaneaterRect.bottom()-10);
        break;
    case 2:
        vbeaneaterRect.setTop(vbeaneaterRect.top()+10);
        vbeaneaterRect.setBottom(vbeaneaterRect.bottom()+10);
        break;
    case 3:
        vbeaneaterRect.setLeft(vbeaneaterRect.left()-10);
        vbeaneaterRect.setRight(vbeaneaterRect.right()-10);
        break;
    case 4:
        vbeaneaterRect.setLeft(vbeaneaterRect.left()+10);
        vbeaneaterRect.setRight(vbeaneaterRect.right()+10);
        break;
    default:;
    }
    if(vbeaneaterRect.top()<20&&vbeaneaterRect.left()>220&&vbeaneaterRect.left()<270){
        vbeaneaterRect.setBottom(vbeaneaterRect.bottom()+460);
        vbeaneaterRect.setTop(vbeaneaterRect.top()+460);
    }
    if(vbeaneaterRect.bottom()>490&&vbeaneaterRect.left()>220&&vbeaneaterRect.left()<270){
        vbeaneaterRect.setBottom(vbeaneaterRect.bottom()-460);
        vbeaneaterRect.setTop(vbeaneaterRect.top()-460);
    }
    if(vbeaneaterRect.left()<20&&vbeaneaterRect.top()>220&&vbeaneaterRect.top()<270){
        vbeaneaterRect.setLeft(vbeaneaterRect.left()+460);
        vbeaneaterRect.setRight(vbeaneaterRect.right()+460);
    }
    if(vbeaneaterRect.right()>490&&vbeaneaterRect.top()>220&&vbeaneaterRect.top()<270){
        vbeaneaterRect.setLeft(vbeaneaterRect.left()-460);
        vbeaneaterRect.setRight(vbeaneaterRect.right()-460);
    }
    if((vbeaneaterRect.top()<220||vbeaneaterRect.top()>270)&&(vbeaneaterRect.top()<20||vbeaneaterRect.bottom()>490)){
        blsOver=true;
        sDisplay="GAME OVER";
    }
    if((vbeaneaterRect.top()<220||vbeaneaterRect.top()>270)&&(vbeaneaterRect.left()<20||vbeaneaterRect.right()>490)){
        blsOver=true;
        sDisplay="GAME OVER";
    }
    update();
}
QRect beaneater::CreateRect(){
    int x,y;
    x=qrand()%3;
    y=qrand()%3;
    QRect rect(20+x*220,20+y*220,20,20);
    return rect;
}
void beaneater::deathTimeout()
{
    deathCountdown--;
    if(deathCountdown <= 0)
    {
        timer->stop();
    }
}

void beaneater::IsEat(){
    if(vbeaneaterRect==food){
        nScore+=10;
        food=CreateRect();
    }
}

